﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.NhomQuyen;
using DevExpress.XtraEditors.Repository;
using RTSoft.SoureCode.Control.Quyen;
using RTSoft.Modules;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmNhomQuyen : DevExpress.XtraEditors.XtraForm
    {
        public frmNhomQuyen()
        {
            InitializeComponent();
        }

        DataTable tbl;
        String errConnect;
        String errSQL;
        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
             try{
				nhomquyenctrl ctrl = new nhomquyenctrl();
				nhomquyeninfo info = new nhomquyeninfo();	
				errConnect = "";
				errSQL = "";
				DataRow row = GridView1.GetDataRow(e.RowHandle);
				info.ID_NHOM = Convert.ToInt32(RTValidate.CheckNull(row["ID_NHOM"]));
				info.ID_QUYEN = Convert.ToInt32(RTValidate.CheckNull(row["ID_QUYEN"]));
				info.TEN_NHOM = RTValidate.CheckNull(row["TEN_NHOM"]).ToString();
				info.TRANGTHAI = 1;
                row["NGAY_TAO"] = DateTime.Now;
				info.NGAY_TAO = Convert.ToDateTime(RTValidate.CheckNull(row["NGAY_TAO"]));
				if (info.ID_NHOM != 0)
				{
					Boolean result = Library.ShowQuestionMessage("Dữ liệu có sự thay đổi bạn có chắc muốn sửa");
					if (result != false)
					{
						ctrl.Update(ref errConnect, ref errSQL, info);
					}
				}
				else
				{
					ctrl.Insert(ref errConnect, ref errSQL, info);
				}
				this.InitGrid();   
			}catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        private void frmNhomQuyen_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch
            {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            }
        }

        private void InitGrid()
        {
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            nhomquyenctrl ctrl = new nhomquyenctrl();
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;            
            GridView1.Columns["ID_QUYEN"].Caption = "Quyền";
            GridView1.Columns["TEN_NHOM"].Caption = "Chức Vụ";           
            GridView1.Columns["NGAY_TAO"].Caption = "Ngày Tạo";
            GridView1.Columns["TRANGTHAI"].Visible = false;
            GridView1.Columns["ID_NHOM"].Visible = false;
            // Load danh sách quyền lên grid
            RepositoryItemGridLookUpEdit riCombo = new RepositoryItemGridLookUpEdit();
            quyenctrl qctrl = new quyenctrl();
            DataTable tblQuyen = new DataTable();
            qctrl.getData(ref errConnect, ref errSQL, ref tblQuyen);
            riCombo.DataSource = tblQuyen;
            riCombo.ValueMember = "ID_QUYEN";
            riCombo.DisplayMember = "TEN_QUYEN";
            riCombo.NullText = "";
            riCombo.View.Columns["TEN_QUYEN"].Caption = "Tên Quyền";
            riCombo.View.Columns["ID_QUYEN"].Visible = false;
            riCombo.View.Columns["TRANGTHAI"].Visible = false;
            riCombo.View.Columns["NGAY_TAO"].Visible = false;
            GridControl1.RepositoryItems.Add(riCombo);
            GridView1.Columns["ID_QUYEN"].ColumnEdit = riCombo;

            // chỉnh cột ngày tạo thành dateTime
            RepositoryItemDateEdit riDate = new RepositoryItemDateEdit();
            riDate.ShowClear = true;
            riDate.ShowToday = true;
            riDate.EditMask = "dd/MM/yyyy";
            riDate.Mask.UseMaskAsDisplayFormat = true;
            GridView1.Columns["NGAY_TAO"].ColumnEdit = riDate;
            GridControl1.RepositoryItems.Add(riDate);
            GridView1.Columns["NGAY_TAO"].ColumnEdit = riDate;
            GridView1.BestFitColumns();		
        }

        #region "Event Button"
        private void btnExcel_Click(object sender, EventArgs e)
        {
            Library.ExportToExcel(this, "Danh Sách Chi Tiết Quyền", GridView1);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (GridView1.FocusedRowHandle >= 0)
                {
                    int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_NHOM"].FieldName));
                    errConnect = "";
                    errSQL = "";
                    string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_QUYEN"].FieldName).ToString();
                    Boolean result = Library.ShowQuestionMessage("Bạn có chắc chắn xóa " + strName + " ?");
                    if (result == false)
                    {
                        return;
                    }
                    nhomquyenctrl ctrl = new nhomquyenctrl();
                    ctrl.Delete(ref errConnect, ref errSQL, id);
                    if (errSQL != "")
                    {
                        Library.ShowMessage("Không thể xóa nhóm quyền này");
                    }
                    else
                    {
                        GridView1.DeleteSelectedRows();
                        GridView1.RefreshData();
                    }
                }
            }
            catch (Exception ex)
            {
                Library.ShowMessage(ex.ToString());
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
       
    }
}